package ys.ewp.facade;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import ys.ewp.dto.CommentDto;
import ys.ewp.dto.EmployeeDto;
import ys.ewp.dto.NoticeDto;
import ys.ewp.po.Comment;
import ys.ewp.po.Employee;
import ys.ewp.po.Notice;
import ys.ewp.service.CommentService;
import ys.ewp.service.EmployeeService;
import ys.ewp.service.NoticeService;

@Component
public class CommentFacade {
	
	@Resource
	private CommentService commentService;
	
	@Resource
	private NoticeService noticeService;
	
	@Resource
	private EmployeeService employeeService;
	
	/**
	 * @功能 根据公告ID获取该公告下的评论
	 */
	public List<CommentDto> getCommentListByNotId(Integer notId){
		List<CommentDto> comDtoList = getDtoListByPoList(commentService.getCommentListByNotId(notId));
		return comDtoList;
		
	}
	
	/**
	 * @功能 针对公告进行评论
	 * @param comDto
	 * @param notDto
	 * @param empDto
	 * @return
	 */
	public List<CommentDto> insertComment(CommentDto comDto, NoticeDto notDto, EmployeeDto empDto){
		System.out.println("进来了");
		Notice not = noticeService.get(notDto.getId());
		Employee emp = employeeService.get(empDto.getId());
		System.out.println(comDto.getContent());
		Comment com = CommentDto.getPoByDto(comDto);
		com.setNotice(not);
		com.setEmployee(emp);
		commentService.save(com);
		List<CommentDto> comDtoList = this.getDtoListByPoList(commentService.getCommentListByNotId(notDto.getId()));
		return comDtoList;
	}
	
	/**
	 * @功能 根据评论PO列表获取评论DTO列表
	 * @param comList 评论PO列表
	 * @return 评论DTO列表
	 */
	private List<CommentDto> getDtoListByPoList(List<Comment> comList){
		
		if(comList == null || comList.isEmpty()){
			return Collections.emptyList();
		}
		List<CommentDto> comDtoList = new ArrayList<CommentDto>();
		for(Comment com : comList){
			CommentDto comDto = null;
			comDto = CommentDto.getDtoByPo(com);
			
			comDtoList.add(comDto);
		}
		return comDtoList;
	}
	
}
